package com.sgb.kjwl.view.ui.fragment.material;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentSupplierCollectionBinding;
import com.sgb.jicai.event.JiCaiEventEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.jicai.view.fragment.FactoryCentralizedFragment;

import org.greenrobot.eventbus.EventBus;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SupplierCollectionFragment#newInstance} factory method to
 * create an instance of this fragment.
 * app首页商品模块的item供方采集fragment(包含搜索框)
 *
 * @author by hq 2021.04.03
 */
public class SupplierCollectionFragment extends AppLazyFragment {

    private FragmentSupplierCollectionBinding binding;
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_POSITION = "param1";
    private static final String ARG_TYPE = "param2";
    private boolean isOnResumed = false;
    // TODO: Rename and change types of parameters
    private int position;
    private String type;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param position Parameter 1.
     * @param type     Parameter 2.
     * @return A new instance of fragment SupplierCollectionFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SupplierCollectionFragment newInstance(int position, String type) {
        SupplierCollectionFragment fragment = new SupplierCollectionFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_POSITION, position);
        args.putString(ARG_TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            position = getArguments().getInt(ARG_POSITION);
            type = getArguments().getString(ARG_TYPE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_supplier_collection, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onMultiClick(View v) {
        switch (v.getId()) {
            case R.id.search_tv:
                informSearch();
                break;
            default:
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isOnResumed) {
            initView();
            isOnResumed = true;
        }
    }

    @Override
    protected void initView() {
        FragmentManager fragmentManager = getParentFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        FactoryCentralizedFragment factoryCentralizedFragment = FactoryCentralizedFragment.newInstance(position, type);
        fragmentTransaction.add(R.id.frame_layout, factoryCentralizedFragment);
        fragmentTransaction.commit();
        binding.searchTv.setOnClickListener(this);
        binding.editSearch.setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                informSearch();
            }
            return false;
        });
    }

    private void informSearch() {
        EventBus.getDefault().post(new JiCaiEventEntity(EventConstant.EVENT_JICAI_HOME_FACTORY, binding.editSearch.getText().toString().trim()));
    }

    @Override
    protected void onLazyLoad() {

    }

}